package org.apache.ofbiz.extend.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.ofbiz.base.util.UtilHttp;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.service.ServiceUtil;

/**
 * 
 * @author YUANPF
 * @date 2019-05-25
 */
public class StrKit {


	public static final String UNDERLINE = "_";
	
	/**
	 * 生成Pascal,根据‘_’分割,首字母大写
	 * @param str
	 * @return
	 */
	public static String unlineToPascal(String str) {
		if (UtilValidate.isNotEmpty(str)) {
			StringBuilder result = new StringBuilder();
			String[] temp = str.split(UNDERLINE);
			for (int i = 0; i < temp.length; i++) {
				if ((temp[i].equals("")) || (temp[i].isEmpty())) {
					continue;
				}
				result.append(fristToUpLaterToLow(temp[i]));
			}
			return result.toString();
		}

		return str;
	}

	/**
	 * 生成驼峰,根据‘_’分割,第一个字母小写
	 * @param str
	 * @return
	 */
	public static String unlineToCamel(String str) {
		if (UtilValidate.isNotEmpty(str)) {
			StringBuilder result = new StringBuilder();
			String[] temp = str.split(UNDERLINE);
			boolean falg = false;
			for (int i = 0; i < temp.length; i++) {
				if ((temp[i].equals("")) || (temp[i].isEmpty())) {
					continue;
				}
				if (!falg) {
					falg = true;
					result.append(temp[i].toLowerCase());
				} else {
					result.append(fristToUpLaterToLow(temp[i]));
				}
			}
			return result.toString();
		}

		return str;
	}
	
	/**
     * 驼峰格式字符串转换为下划线格式字符串
     * @param param
     * @return
     */
    public static String camelToUnline(String str) {
    	if (UtilValidate.isEmpty(str)) {
            return "";
        }
    	str = fristToLoCase(str);
        int len = str.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

	/**
	 * 首字母大写，后续字母小写
	 * @param str
	 * @return
	 */
	public static String fristToUpLaterToLow(String str) {
		if ((str != null) && (str.length() > 1)) {
			str = new StringBuilder().append(str.substring(0, 1).toUpperCase()).append(str.substring(1).toLowerCase())
					.toString();
		}
		return str;
	}

	/**
	 * 首字母小写，后续字母大写
	 * @param str
	 * @return
	 */
	public static String fristToLowLaterToUp(String str) {
		if ((str != null) && (str.length() > 1)) {
			str = new StringBuilder().append(str.substring(0, 1).toLowerCase()).append(str.substring(1).toUpperCase())
					.toString();
		}

		return str;
	}

	public static String fristToUpCase(String str) {
		if ((str != null) && (str.length() > 1)) {
			str = new StringBuilder().append(str.substring(0, 1).toUpperCase()).append(str.substring(1)).toString();
		}
		return str;
	}

	public static String fristToLoCase(String str) {
		if ((str != null) && (str.length() > 1)) {
			str = new StringBuilder().append(str.substring(0, 1).toLowerCase()).append(str.substring(1)).toString();
		}
		return str;
	}
	
	/**
	 * @param elements
	 * @return repeat element 返回重复的元素
	 */
	public static Set<String> getRepeatElement(List<String> elements) {
		Set<String> repeatElement = new HashSet<String>();
		Set<String> set = new HashSet<String>();
		for (String element : elements) {
			if (!set.add(element)) {
				repeatElement.add(element);
			}
		}
		return repeatElement;
	}
	
	/**
	 * @param list 
	 * @return remove element 返回没有重复的元素列表
	 */
	public static List getNoRepeatElementList(List list) {
		Set set = new HashSet();
		List newList = new ArrayList();
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			Object element = iter.next();
			if (set.add(element))
				newList.add(element);
		}
		list.clear();
		list.addAll(newList);
		return list;
	}
	
	public static Set getNoRepeatElementSet(List list) {
		Set set = new LinkedHashSet<>(list);
		return set;
	}
	
	public static String get32UUID() {
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}
	
	/**
	 * 字符串前填充以满足固定长度.
	 */
	public static String fillStrBefore(String string, String fill, int size) {
		StringBuilder sb = new StringBuilder();
		int len = string.length();
		for (int i = 0; i < size - len; i++) {
			sb.append(fill);
		}
		return sb.append(string).toString();
	}
	
	
	public static void main(String[] args) {
		//生成驼峰，首字母大写
		//System.out.println(unlineToPascal("user_name_id"));//UserNameId
		//驼峰，第一个单词小写
		//System.out.println(unlineToCamel("user_name_id"));//userNameId
		//首字母大写，其他小写
		//System.out.println(fristToUpCaseLaterToLoCase("user_name_id"));//User_name_id
		//首字母小写,其他大写
		//System.out.println(fristToLoCaseLaterToUpCase("User_name_id"));//uSER_NAME_ID
		//System.out.println(fillStrBefore("hello","0",10));
		/*Map<String, Object> results = ServiceUtil.returnSuccess();
        String queryString = UtilHttp.urlEncodeArgs(results);
        System.out.println(queryString);*/
        
	}

}
